package dw2;
import java.awt.*;
import java.util.Random;
import java.util.ArrayList;
public class NPC
{
    protected ArrayList<DWGraphics> graphics = new ArrayList<DWGraphics>();
   
    protected int graphics_index;
    
    //Got to set these values to something good
    
    protected int FLEE_SPEED = 3;
    protected int ATTACK_SPEED = 0;
    protected int ATTACK_RANDOM = 50;
    protected int ATTACK_BULLET_SPEED = 10;
    protected int last_time_fired;
    protected int death_particle_count = 100;
    protected int x, y;
    protected Color npc_color;
    protected Random random;
    protected int direction;
    protected int bullet_size = 4;
    

    protected int life;
    //Attributes to be shared with subclass
    protected int WALK_SPEED = 2;
    protected int ATTACK_POWER = 3;
    protected int FIRING_RATE = 1000;
    protected int state;
    //How much points to the worm score does this npc add

    protected Worm player;
    protected int bulletY;
    protected int bulletX;
    protected int points;
    protected int fireCount;




   public NPC(int x, int y, int direction_to_face,int points)
    {
        npc_color = Color.WHITE;
        direction = direction_to_face;
        random = new Random();
        this.x = x;
        this.y = y;
        graphics_index = 0;
        this.points = points;
        state = 0;
    }

    public Rectangle getBounds()
    {
        return graphics.get(graphics_index).getBounds();
    }


    public void action(Main mainparent,Worm player)
    {
        this.player = player;
        move(mainparent,player.getHeadPosition());

        fire_gun(mainparent,player.getHeadPosition());

        

        if(player.hitHead(graphics.get(graphics_index).getBounds())==true)
        {
            //new AePlayWave("Sounds\\bit1.wav").start();
            //Play bite sound
          //  mainparent.bite1HardCrunch.play();
            mainparent.killCount++;

            //Combo manager: class to b written.....
        /*    if(mainparent.comboMode) {
                if(mainparent.comboCount == 0){
                    mainparent.comboSound = mainparent.firstBlood;
                    mainparent.comboCount++;
                }else if(mainparent.comboCount == 1){
                    mainparent.comboSound = mainparent.doubleKill;
                    mainparent.comboCount++;
                }else if(mainparent.comboCount == 2){
                    mainparent.comboSound = mainparent.rampage;
                    mainparent.comboCount = 0;
                }
                mainparent.comboSound.play();
            }

            if(mainparent.killCount%5 == 0) mainparent.dominating.play();
            if(mainparent.killCount%7 == 0) mainparent.excellentSound.play();
*/
            player.add_points(points);
            
            deathParticles(mainparent,player);
            mainparent.removeNPC(this);
            return;
        }
        
    }
    public boolean fire_gun(Main mainparent,Point head_pos){
        bulletY = y + 25;
        bulletX = x + 5;
        if(head_pos.y < mainparent.getHeight()/2 && x > player.getXpos() && direction == 1)
        {
           
            fireCount++;

            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,bulletX, bulletY, Color.black, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        if(head_pos.y < mainparent.getHeight()/2 && x < player.getXpos() && direction == 0)
        {
            fireCount++;
            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,bulletX, bulletY, Color.black, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        return false;
    }

    public void deathParticles(Main mainparent,Worm player)
    {
        int center_x = x + graphics.get(graphics_index).getWidth()/2;
        int center_y = y + graphics.get(graphics_index).getHeight();
        for(int w = 0;w<death_particle_count;w++)
        {
            mainparent.addParticle(new Particle(center_x,center_y,Color.RED,2,5));
        }
                        
    }
    
     public int Chase(){
         int Dis = Math.abs(x - player.getXpos());
        if(player.getAboveGround() && x > player.getXpos() && Dis > 150){
           if(direction == 0){
               direction = 1;
               return 1;
           }
        }

         if(player.getAboveGround() && x < player.getXpos() && Dis > 150){
           if(direction == 1){
               direction = 0;
               return 0;
           }
        }
        return -1;
    }
    public void move(Main mainparent,Point head_pos)
    {
        
        Chase();
        //need to change this
        if(direction == 1)
            x -= WALK_SPEED;
        else
            x += WALK_SPEED;

        if(x < -64 && direction == 1)
            x = mainparent.getMaxWidth();
        if(x > mainparent.getMaxWidth() && !(direction == 1))
            x = -64;
    }

    public void draw(Graphics g,Main mainparent)
    {
        int ymove = 0;
        int move = mainparent.getSize().width/2;
        int shakeamount = mainparent.getShakeAmount();
        move -= mainparent.getScroll();
        
        if(mainparent.isShaking())
        {
            move += (random.nextInt(shakeamount*2)-shakeamount);
            ymove = (random.nextInt(shakeamount*2)-shakeamount);
        }
        graphics.get(graphics_index).draw(g,move+x,ymove+y,direction,0);

    }

    public int getAtPwr(){
        return ATTACK_POWER;
    }

    public double getDirection() {
        return direction;
    }
}


